<template>
  <div>
    <div class="search-container">
      <!--      <el-input v-model="form.msgType" class="search-wrap" />-->
      <!--      <el-button type="primary" size="middle" icon="search" @click="getList">查询</el-button>-->
      <el-button type="primary" size="middle" icon="add" @click="handleAdd">新增</el-button>
    </div>

    <el-table :data="list" border>
      <el-table-column prop="name" label="业务名称" />
      <el-table-column prop="msgType" label="编码" />

      <el-table-column prop="wxTemplateMsgId" align="left" label="默认模板" width="300px">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEditTemplate(scope.row.wxTemplateMsgId)">【{{ scope.row.templateTitle }}】{{
            scope.row.templateIdShort }}</el-button>
        </template>
      </el-table-column>

      <el-table-column prop="templateType" align="center" label="模板类型" width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.templateType === 0">公众号</span>
          <span v-if="scope.row.templateType === 1">小程序</span>
        </template>
      </el-table-column>

      <el-table-column prop="description" label="说明" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column align="center" label="操作" width="200px">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px "
      background
      :current-page.sync="pageNum"
      :page-size.sync="pageSize"
      layout="total, prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />

    <el-dialog v-loading="loading" title="模板映射" :visible.sync="dialogVisible" :destroy-on-close="true" width="620px" style="padding: 20px">
      <el-form ref="form" :model="form" label-position="left" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="业务名称" />
        </el-form-item>

        <el-form-item label="编码" prop="msgType">
          <el-input v-if="isAdd" v-model="form.msgType" placeholder="业务编码" />
          <el-input v-else v-model="form.msgType" readonly placeholder="业务编码" />
        </el-form-item>

        <el-form-item label="默认模板" prop="wxTemplateMsgId">
          <div v-if="templateInfo">【{{ templateInfo.templateIdShort }}】{{ templateInfo.title }}</div>
          <el-button type="plain" size="small" @click="handleSelect">选择</el-button>
        </el-form-item>

        <el-form-item label="说明" prop="description">
          <el-input v-model="form.description" placeholder="说明" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel()">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
    <template-msg-dialog ref="templateMsg" />
  </div>
</template>

<script>
import TemplateMsgDialog from './TemplateMsgDialog'
import {

  templateConfigList,
  addTemplateConfig,
  templateConfigInfo,
  updateTemplateConfig,
  deleteTemplateConfig
} from '@/api/wxTemplateMsg'

export default {
  name: 'TemplateMsgConfig',
  components: {
    TemplateMsgDialog
  },
  data() {
    return {
      pageNum: 0,
      pageSize: 10,
      activeName: 'config',
      dialogVisible: false,
      loading: false,
      list: [],
      total: 0,
      templateInfo: null,
      isAdd: true,
      rules: {
        name: [{
          min: 1,
          max: 20,
          required: true,
          message: '请输入业务名称, 长度在 1 到 20 个字符',
          trigger: 'blur'
        }
        ],
        msgType: [{
          min: 3,
          max: 255,
          required: true,
          message: '请输入业务编码, 长度在 3 到 20 个字符',
          trigger: 'blur'
        }],
        wxTemplateMsgId: [{
          required: true,
          message: '请选择微信模板',
          trigger: 'blur'
        }]

      },
      form: {
        name: '',
        msgType: '',
        description: ''
      },
      templateMsgTypeList: []
    }
  },
  mounted() {
    this.getList()
  },

  methods: {
    getList() {
      templateConfigList({ pageNum: this.pageNum, pageSize: this.pageSize }).then((res) => {
        this.list = res.data.list
        this.total = res.data.total
        return res
      })
    },

    handleAdd() {
      this.templateInfo = null
      this.form = {
        name: '',
        msgType: '',
        businessId: 0,
        description: ''
      }
      this.dialogVisible = true
      this.isAdd = true
    },

    handleEditTemplate(id) {
      this.$router.push({ name: 'editTemplateMsg', params: { id: id } })
    },
    handleEdit(id) {
      this.loading = true
      templateConfigInfo(id).then(resp => {
        this.loading = false
        if (resp.code === 0) {
          this.templateInfo = {
            title: resp.data.templateTitle,
            templateIdShort: resp.data.templateIdShort
          }
          this.form = resp.data
          this.dialogVisible = true
          this.isAdd = false
        }
      }).catch(() => {
        this.loading = false
      })
    },

    handleCancel() {
      this.dialogVisible = false
    },
    handleCurrentChange() {
      this.getList()
    },

    handleSelect() {
      this.$refs.templateMsg.show().then(template => {
        this.templateInfo = template
        this.form.wxTemplateMsgId = template.id
        this.form.templateType = template.templateType
      })
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTemplateConfig(id).then((res) => {
          if (res.code === 0) {
            this.getList()
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
    doAdd(data) {
      addTemplateConfig(data).then(resp => {
        this.loading = false
        if (resp.code === 0) {
          this.$message.success('添加成功！')
          this.dialogVisible = false
          this.getList()
        } else {
          this.$message.error(resp.msg)
        }
      }).catch(error => {
        this.loading = false
      })
    },

    doSave(data) {
      updateTemplateConfig(data).then(resp => {
        this.loading = false
        if (resp.code === 0) {
          this.$message.success('修改成功！')
          this.dialogVisible = false
          this.getList()
        } else {
          this.$message.error(resp.msg)
        }
      }).catch(error => {
        this.loading = false
      })
    },

    handleSave(id) {
      this.loading = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = this.form
          if (this.isAdd) {
            this.doAdd(data)
          } else {
            this.doSave(data)
          }
        } else {
          this.loading = false
          this.$message.error('请按要求填写好表格.')
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .search-container {
    margin-top: 20px;
    margin-bottom: 20px
  }

  .search-wrap {
    width: 300px;
    margin-right: 10px
  }
  .el-button--text{
	  color: $btnBgc;
  }
  .el-button--primary{
  	  background-color: $btnBgc;
  	  border-color: $btnBgc;
  }
  .el-button--primary:hover{
	  opacity: 0.5;
  }
  .el-button--small:Hover{
	  background-color: $btnBgc;
	  border-color: $btnBgc;
	  color: white;
  }
  ::v-deep .el-input__inner:focus{
	  border-color: $btnBgc;
  }
  ::v-deep .el-dialog__headerbtn .el-dialog__close:hover{
	  color: $btnBgc;
  }
</style>
